﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Common.Utility;
using System.Data;
using System.Data.Common;

namespace Runda.CMS.Dal
{
    public partial class WsmMaintainBillDal:DalBase
    {
        public void Update(Enumerator.AccessoryReplacementCheckStatus status, Guid MaintainID, Guid CheckerID, string CheckerName, string AccessoryReplaceMemo)
        {
            string sql = "update Cms_Wsm_MaintainBill set AccessoryCheckStatus = @AccessoryCheckStatus,CheckerID=@CheckerID,CheckerName=@CheckerName,CheckAt = getdate(),AccessoryReplaceMemo=@AccessoryReplaceMemo where MaintainID = @MaintainID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@AccessoryCheckStatus", DbType.Int32, (int)status);
            CurrentDatabase.AddInParameter(dbcommand, "@MaintainID", DbType.Guid, MaintainID);
            CurrentDatabase.AddInParameter(dbcommand, "@CheckerID", DbType.Guid, CheckerID);
            CurrentDatabase.AddInParameter(dbcommand, "@CheckerName", DbType.String, CheckerName);
            CurrentDatabase.AddInParameter(dbcommand, "@AccessoryReplaceMemo", DbType.String, AccessoryReplaceMemo);
            
            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

        public void Update(Enumerator.InstrumentAssmblyStatus status, Guid MaintainID, Guid CheckerID, string CheckerName)
        {
            string sql = "update Cms_Wsm_MaintainBill set AccessoryCheckStatus = @AccessoryCheckStatus,CheckerID=@CheckerID,CheckerName=@CheckerName,CheckAt = getdate() where MaintainID = @MaintainID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@AccessoryCheckStatus", DbType.Int32, (int)status);
            CurrentDatabase.AddInParameter(dbcommand, "@MaintainID", DbType.Guid, MaintainID);
            CurrentDatabase.AddInParameter(dbcommand, "@CheckerID", DbType.Guid, CheckerID);
            CurrentDatabase.AddInParameter(dbcommand, "@CheckerName", DbType.String, CheckerName);

            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

        public void CancelUpdate(Guid registerID)
        { 
            string sql =@"update Cms_Wsm_AccessoryReplacement set CheckDate = null,CheckStatus = 0 where MaintainID in(select MaintainID from Cms_Wsm_MaintainBill where RegisterID = @RegisterID);
                update Cms_Wsm_MaintainBill set CheckAt = null,CheckerID = null,AccessoryCheckStatus = 0 where RegisterID = @RegisterID;
                update Cms_Wsm_TaskRegister set TaskStatus = 0 where RegisterID = @RegisterID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@RegisterID", DbType.Guid, registerID);
            
            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }
        public DataTable QueryMaintainBll(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            string sqlSb = @"select * from (select c.AccessoryID,A.*,case when checkerid is null then 0 else 1 end as checkStatus ,B.CustomerID,B.TaskType,B.ExpectEndDate,B.InstrumentID,B.IsSystemAssign
From Cms_Wsm_MaintainBill A 
join Cms_Wsm_TaskRegister B on A.RegisterID = B.RegisterID
left join
(
    select MaintainID,count(*) as AccessoryID from Cms_Wsm_AccessoryReplacement group by MaintainID
) c on c.MaintainID = a.MaintainID) as TT
where  1=1 and Deleted = 0 {0} ";

            string sqlStr = string.Format(sqlSb.ToString(), WhereClause);

            string CmdText = @"WITH T AS
(
SELECT ROW_NUMBER() OVER(ORDER BY {2} ) AS row_number, * 
  FROM ({0}) as A
)
SELECT * from T WHERE row_number > @StartRowNum AND  row_number <= @EndRowNum
select count(*) from ({1}) as B 
";

            CmdText = string.Format(CmdText, sqlStr, sqlStr, OrderBy);

            DbCommand dbCommand = this._db.GetSqlStringCommand(CmdText);
            CurrentDatabase.AddInParameter(dbCommand, "@StartRowNum", System.Data.DbType.Int32, (PageIndex - 1) * PageSize);
            CurrentDatabase.AddInParameter(dbCommand, "@EndRowNum", System.Data.DbType.Int32, PageIndex * PageSize);

            DataSet ds = CurrentDatabase.ExecuteDataSet(dbCommand);
            RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            return ds.Tables[0];
        }

        public int IsExistsMaintainBill(string BillNO)
        {
            string sqlSb = @"select count(*) from Cms_Wsm_MaintainBill where PaperInvoicesNo= '{0}'";
            string sqlStr = string.Format(sqlSb.ToString(), BillNO);

            DbCommand dbCommand = this._db.GetSqlStringCommand(sqlStr);

            DataSet ds = CurrentDatabase.ExecuteDataSet(dbCommand);
            int RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            return RecordCount;

        }

        //更新保内配件入库状态
        public int updateAccessoryReplacement(string maintainBillNo, string AccessoryID)
        {
            string sqlSb = @"declare @maintainID uniqueidentifier;select @maintainID = maintainID from Cms_Wsm_MaintainBill where PaperInvoicesNo = '{0}'; update Cms_Wsm_AccessoryReplacement set BillOrderStatus=2 where MaintainID = @MaintainID and AccessoryID = '{1}';";

            string sqlStr = string.Format(sqlSb.ToString(), maintainBillNo, AccessoryID);
            DbCommand dbCommand = this._db.GetSqlStringCommand(sqlStr);
            int irtn = CurrentDatabase.ExecuteNonQuery(dbCommand);
            return irtn;
        }
        //生成注册单
        public void genData()
        {
            DbCommand dbcommand = CurrentDatabase.GetStoredProcCommand("CreateBillForCycle");
            dbcommand.CommandTimeout = 30000000;

            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }
    }
}
